﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Web;
using System.Web.Mvc;

namespace BaseLib.Localiztion
{
    public static class Translator
    {
        private const string CULTURE_COOKIE_NAME = "__gil_culture_cookie";
        private static string _defaultCulture = "en-us";

        public static void SetApplicationDefaultCulture(string culture)
        {
            _defaultCulture = culture;
        }

        public static string Translate(string key, params string[] args)
        {
            if (args.Any())
                return string.Format(LocalizationEngine.GetResource(key, GetCurrentCulture()), args);
            else
                return LocalizationEngine.GetResource(key, GetCurrentCulture());
        }

        public static string GetCurrentCulture()
        {
            string culture = _defaultCulture;
            if (HttpContext.Current.Session == null)
                return culture;

            var cultureObj = HttpContext.Current.Session[CULTURE_COOKIE_NAME];

            if (cultureObj != null)
                culture = cultureObj.ToString();

            return culture;
        }

        public static MvcHtmlString Translate(this HtmlHelper html, string resoureKey, params string[] args)
        {
            return MvcHtmlString.Create(Translator.Translate(resoureKey, args));
        }

        public static void Init(string resourcesPath, string defaultCulture = "en-US")
        {
            LocalizationEngine.Init(resourcesPath);

            Translator.SetApplicationDefaultCulture(defaultCulture);
        }

    }
}
